/*******************************************************************************
 * Copyright (c) 2013 Zero Square LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Zero Square LLC. Version 1
 * which accompanies this distribution
 ******************************************************************************/
package com.zsqr.dashboard.services.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.NoResultException;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Iterator;
import com.zsqr.dashboard.dao.UserCardDao;
import com.zsqr.dashboard.domain.UserCardEntity;
import com.zsqr.dashboard.services.UserCardService;


public class UserCardServiceImpl implements UserCardService{

	private UserCardDao userCardDao; 
	
	/**
	 * Give list of cards for a user
	 * @param userName from UserCardEntity
	 * @return list of card for a user if success 
	 */
	//@XmlElement(name="card")
	public List<UserCardEntity> userCardLists = new ArrayList<UserCardEntity>();
	public List<UserCardEntity> getUserCard(String userName){	
		
		userCardLists.addAll(userCardDao.loadCardsByUserName(userName));
		return userCardLists;
	}

	public UserCardDao getUserCardDao() {
		return userCardDao;
	}

	public void setUserCardDao(UserCardDao userCardDao) {
		this.userCardDao = userCardDao;
	}
	
	public boolean AddCard(UserCardEntity userCardEntity){
		if(userCardDao.save(userCardEntity)==null){
			return false;
		}
		return true;
		
	}
	
	public boolean delCard(String cardNumber)
	{
		UserCardEntity cardEntity = this.userCardDao.getCardByCardNumber(cardNumber);
		if(cardEntity==null)
		{
			System.out.print("No card witht this  number is present in DB");
			return false;
		}
		try{
			this.userCardDao.delete(cardEntity);
		}catch(NoResultException e){
			return false;
		}
		
		return true;
	}

	public List<UserCardEntity> getUserCardList() {
		return userCardLists;
	}

	public void setUserCardList(List<UserCardEntity> userCardList) {
		this.userCardLists.addAll(userCardList);
	}
	
    public boolean saveCardList(List<UserCardEntity> cardList)
    {
    	//List<String> lList = Arrays.asList(sArray);
    	 
    	// iterator loop
    	//System.out.println("#1 iterator");
    	Iterator<UserCardEntity> iterator = cardList.iterator();
    	while (iterator.hasNext()) {
    		System.out.println(iterator.next());
    		UserCardEntity userCardEntity = iterator.next();
    		if(userCardDao.save(userCardEntity)==null){
    			return false;
    		}
    			
    	}
    	
    	return true;
    }
    

}
